
#include "native_delegate.h"
#include <string>

jint native_delegate::init(JNIEnv *env, jobject ctx) {
    if (ctx == nullptr) {
        return JNI_ERR;
    }

    if (context == nullptr) {
        // 转成 application context
        jclass clazz = env->GetObjectClass(ctx);
        jmethodID jmi = env->GetMethodID(
                clazz,
                "getApplicationContext",
                "()Landroid/content/Context;"
        );

        this->context = env->CallObjectMethod(ctx, jmi);
    }
    return JNI_OK;
}

jstring native_delegate::sign(JNIEnv *env, jstring str) {

    if (context == nullptr
        || str == nullptr
        || env->GetStringUTFLength(str) == 0) {

        return env->NewStringUTF("");
    }

    std::string result = "Sign by ndk: ";
    const char *chars = env->GetStringUTFChars(str, nullptr);
    result.append(chars);
    env->ReleaseStringUTFChars(str, chars);

    return env->NewStringUTF(result.c_str());
}
